<?php
class FormUser extends CFormModel
{

	public $username = null;
	public $firstname = null;
	public $lastname = null;
	public $password = null;
	public $sex = 0;
	public $email = null;
	public $activkey = null;
	public $createtime = null;
	public $lastvisit = 0;
	public $country_id = 0;
	public $superuser = 0;
	

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		
		return array(
			array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => UserModule::t("Incorrect symbols (A-z0-9).")),
            array('username,firstname,lastname, password,email', 'required'),
			
            array('email', 'email'),
            array('username', 'checkUserName'),
            array('email', 'checkEmail'),
            // array('password', 'authenticate'),
        );
	}
	public function checkUserName($attribute,$params)
    {
		$user = User::model()->findByAttributes(array(
			'username'=>$this->username
		));
        if(!empty($user)) $this->addError('username',"This user's username already exists.");
        
    }
	public function checkEmail($attribute,$params)
    {
		$user = User::model()->findByAttributes(array(
			'email'=>$this->email
		));
        if(!empty($user)) $this->addError('email',"This user's email address already exists.");
        
    }
	public function attributeLabels()
	{
		return array(
			'username'=>UserModule::t("User name"),
			'password'=>UserModule::t("Password"),
			'verifyPassword'=>UserModule::t("Retype Password"),
			'email'=>UserModule::t("E-mail"),
			'verifyCode'=>UserModule::t("Verification Code"),
			'id' => UserModule::t("Id"),
			'activkey' => UserModule::t("activation key"),
			'createtime' => UserModule::t("Registration date"),
			'lastvisit' => UserModule::t("Last visit"),
			'country_id' => UserModule::t("Country"),
			'superuser' => UserModule::t("Superuser"),
			'status' => UserModule::t("Status"),
			'roles' => UserModule::t("Roles"),
		);
	}
	public function relations()
	{
    	Yii::import('application.modules.countries.models.Country');
		$relations = array(
			'country'=>array(self::BELONGS_TO, 'Country', 'country_id'),
		);
		return $relations;
	}
}